'use strict'

const gohttp=require('gohttp')

class login{

    constructor () {
        this.param='/'
    }

    async get (c) {
        let login_url=`https://api.weixin.qq.com/sns/jscode2session`
        +`?appid=${c.service.wxkey.appid}`
        +`&secret=${c.service.wxkey.secret}`
        +`&js_code=${c.query.code}`
        +`&grant_type=authorization_code`
        let result=await gohttp.get(login_url)
        let r=result.json()
        if(r.openid===undefined){
            c.status(500)
            return
        }

        let info={
            openid:r.openid,
            expires:7200000,
            timestamp:Date.now(),
            random:Math.random()
        }
        
        let token=c.helper.aesEncrypt(JSON.stringify(info),c.service.tokenKey)
        //console.log(token)
        c.send(token)
        //c.res.body=token
    }
    async post(c){
      console.log('这是传递给我的用户登录数据：',c.body.toString())
        let data=await c.service.model.wxlogin.insert(c.body.toString())
        c.send(data)
    }
}

module.exports = login
